package com.qf.jie.service.impl;

import com.qf.jie.dao.AdminDao;
import com.qf.jie.dao.CommodityDao;
import com.qf.jie.dao.UserDao;
import com.qf.jie.dao.impl.AdminDaoImpl;
import com.qf.jie.dao.impl.CommodityDaoImpl;
import com.qf.jie.dao.impl.UserDaoImpl;
import com.qf.jie.entity.bo.Page;
import com.qf.jie.entity.po.Admin;
import com.qf.jie.entity.po.Commodity;
import com.qf.jie.entity.po.User;
import com.qf.jie.entity.vo.RespBean;
import com.qf.jie.service.AdminService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    AdminDao adminDao = new AdminDaoImpl();
    UserDao userDao = new UserDaoImpl();
    CommodityDao commodityDao = new CommodityDaoImpl();

    // 1.管理员登录
    @Override
    public RespBean login(HttpServletRequest req, String name, String password) {
        if (name == null || name.equals("") || password == null || password.equals("")){
            return RespBean.respError("用户名和密码不能为空");
        }
        int id = adminDao.getIdByNameAndPassword(name, password);
        List<Admin> admins = adminDao.getBeanById(id);
        if (admins.size() == 0){
            return RespBean.respError("账号或密码错误");
        }
        req.getSession().setAttribute("admin", admins.get(0));
        return RespBean.respSuccess("登录成功");
    }

    @Override
    public RespBean register(Admin admin) {
        return null;
    }

    // 2.管理员获取所有用户信息（分页）
    @Override
    public RespBean getPage(Object currentPage, Object len) {
        // 当前页(默认第一页)，
        int currentPage_Int = 1;
        // 每页长度(默认3条)
        int len_Int = 3;
        try {
            currentPage_Int = Integer.parseInt((String) currentPage);
            len_Int = Integer.parseInt((String) len);
        }catch (Exception e){

        }
        if (len_Int < 1 ){
            len_Int = 1;
        }
        // 数据总量
        int total = userDao.getCount();
        // 总页数
        int pageCount = (int)Math.ceil( (0.0+total)/len_Int);

        if (currentPage_Int < 1){
            currentPage_Int = 1;
        }else if (currentPage_Int > pageCount){
            currentPage_Int = pageCount;
        }

        // 要展示的数据
        List<User> list = userDao.getList((currentPage_Int-1) * len_Int, len_Int);



        Page page = new Page(total, len_Int, pageCount, currentPage_Int, list);
        return RespBean.respSuccess(page);
    }
    // 管理员获取所有用户信息
    @Override
    public RespBean getUserList() {
        List<User> list = userDao.getList();
        if (list.size() == 0){
            return RespBean.respError("未获取到用户信息");
        }
        return RespBean.respSuccess(list);
    }

    // 3.管理员删除用户
    @Override
    public RespBean deleteUser(String strId) {
        try {
            int id = Integer.parseInt(strId);
            int num = userDao.shamDeleteById(id);
            if (num == 0){
                return RespBean.respError("删除失败，未知错误");
            }
            return RespBean.respSuccess("删除成功");
        }catch (Exception e){
        }
        return RespBean.respError("id的类型异常：" + strId);
    }

    // 4.管理员修改用户信息
    public RespBean updateUser(String id, String username, String password, String user_type, String money, String sex, String phone){
        try{
            if (username.equals("") || password.equals("") || sex.equals("") || phone.equals("") ){
                return RespBean.respError("传入的参数有异常：" );
            }
            User user = new User(Integer.parseInt(id), username, password, Integer.parseInt(user_type), Double.parseDouble(money), sex, phone);
            int num = userDao.updateById(user);
            if (num == 0){
                return RespBean.respError("修改失败，未知错误");
            }
            return RespBean.respSuccess("修改成功");
        }catch (Exception e){
            return RespBean.respError("传入的参数有异常：" );
        }
    }

    // 5.管理员查看所有商品
    @Override
    public RespBean getCommodityPageList(Object currentPage, Object len) {
        // 当前页(默认第一页)，
        int currentPage_Int = 1;
        // 每页长度(默认3条)
        int len_Int = 3;
        try {
            currentPage_Int = Integer.parseInt((String) currentPage);
            len_Int = Integer.parseInt((String) len);
        }catch (Exception e){

        }
        if (len_Int < 1 ){
            len_Int = 1;
        }
        // 数据总量
        int total = commodityDao.getCount();
        // 总页数
        int pageCount = (int)Math.ceil( (0.0+total)/len_Int);

        if (currentPage_Int < 1){
            currentPage_Int = 1;
        }else if (currentPage_Int > pageCount){
            currentPage_Int = pageCount;
        }

        // 要展示的数据
        List<Commodity> list = commodityDao.getList((currentPage_Int - 1) * len_Int, len_Int);



        Page page = new Page(total, len_Int, pageCount, currentPage_Int, list);
        return RespBean.respSuccess(page);
    }
    // 6.管理员删除指定商品
    @Override
    public RespBean deleteCommodity(String strId) {
        try {
            int id = Integer.parseInt(strId);
            int num = commodityDao.deleteById(id);
            if (num == 0){
                return RespBean.respError("删除失败，未知错误");
            }
            return RespBean.respSuccess("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.respError("id的类型异常：" + strId);
        }

    }


}
